package com.ssms.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ssms.common.R;
import com.ssms.domain.Counsellor;
import com.ssms.domain.Student;
import com.ssms.domain.Teacher;
import com.ssms.service.CounsellorService;
import com.ssms.service.StudentService;
import com.ssms.service.TeacherService;
import com.ssms.utils.JwtUtils;
import com.ssms.vo.LoginVo;
import com.ssms.vo.paras.LoginParam;
import com.ssms.vo.paras.RegisterPara;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

@RestController

public class PublicController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private CounsellorService counsellorService;
    @PostMapping("/userLogin")
    public R<Object> login(@RequestBody LoginParam loginParam){
        //提交的代码先md5加密
        String password = loginParam.getUserPwd();
        String number = loginParam.getUserName();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //根据客户端提交的名字比对
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getStudentNum,number);
        Student st = studentService.getOne(queryWrapper);

        if (st==null){
            LambdaQueryWrapper<Teacher> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Teacher::getTeacherNum,number);

            Teacher te = teacherService.getOne(queryWrapper1);
            if (te == null){
                LambdaQueryWrapper<Counsellor> queryWrapper2 = new LambdaQueryWrapper<>();
                queryWrapper2.eq(Counsellor::getNum,number);
                Counsellor counsellor = counsellorService.getOne(queryWrapper2);
                if (counsellor == null){
                    return R.error("登录失败，请检查工号和密码！");
                }
                if (!counsellor.getPwd().equals(password)){
                    return R.error("登录失败，密码错误！");
                }
                String token = counsellorService.createToken(counsellor);
                LoginVo loginVo = new LoginVo();
                loginVo.setName(counsellor.getName());
                loginVo.setRole("counsellor");
                loginVo.setToken(token);
                return R.success(loginVo,"登录成功");
            }
            if (!te.getTeacherPwd().equals(password)){
                return R.error("登录失败，密码错误！");
            }
            String token = teacherService.createToken(te);
            LoginVo loginVo = new LoginVo();
            loginVo.setName(te.getTeacherName());
            loginVo.setRole("teacher");
            loginVo.setToken(token);
            return R.success(loginVo,"登录成功");
        }
        if (!st.getStudentPwd().equals(password)){
            return R.error("登录失败，密码错误！");
        }
        String token = studentService.createToken(st);
        LoginVo loginVo = new LoginVo();
        loginVo.setName(st.getStudentName());
        loginVo.setRole("student");
        loginVo.setToken(token);
        return R.success(loginVo,"登录成功");
    }
    @PostMapping("/userRegiste")
    public R<Object> userRegiste(@RequestBody RegisterPara registerPara){
        //提交的代码先md5加密
        String password = registerPara.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        String role = registerPara.getRole();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
        if (role.equals("student")){
            Student st = new Student();
            st.setStudentName(registerPara.getName());
            st.setRole(role);
            //默认班级
            st.setStudentClass("软件2001");
            st.setStudentNum(registerPara.getEmail());
            st.setStudentPwd(password);
            st.setJoinTime(date);
            studentService.save(st);
        }
        else {
            Teacher te = new Teacher();
            te.setJoinTime(date);
            te.setRole(role);
            te.setTeacherName(registerPara.getName());
            te.setTeacherNum(registerPara.getEmail());
            te.setTeacherPwd(password);
            teacherService.save(te);
        }
        return R.success("注册成功");
    }

}
